package client.comm.baoding.adapter

import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.graphics.Paint
import android.text.Layout
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.recyclerview.widget.RecyclerView
import client.comm.baoding.R
import client.comm.baoding.api.bean.GoodsIndex
import client.comm.baoding.databinding.LayoutJxhwItemBinding
import client.comm.baoding.ui.GoodsDetailActivity
import client.comm.baoding.utils.TextTool
import client.comm.commlib.base.BaseListAdapter
import client.comm.commlib.base.BindingViewHoder
import com.bumptech.glide.RequestManager

/**
 * 精选商品
 */
class JxGood2Adapter : BaseListAdapter<GoodsIndex.JxGoods> {

    val manager: RequestManager

    constructor(context: Context, manager: RequestManager) : super(context) {
        this.manager = manager
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {

        return BindingViewHoder(
            DataBindingUtil.inflate(
                layoutInflater,
                R.layout.layout_jxhw_item,
                parent,
                false
            )
        ).apply {

            var itemBinding = binding as LayoutJxhwItemBinding
            with(itemBinding) {
//                val wi = (getScreenWidth(context) / 3) - dipToPx(context.resources, 20.0f)
//                root.layoutParams.width = wi.toInt()
//                root.layoutParams.height = wi.toInt()
            }
        }
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        val bindingViewHolder = holder as BindingViewHoder<ViewDataBinding>
        val binding = bindingViewHolder.binding as LayoutJxhwItemBinding
        val bean = getItem(position)
        binding.bean = bean
        manager.load(bean.goods_img).into(binding.img)

        TextTool.getBuilder("¥").setAlign(Layout.Alignment.ALIGN_CENTER).setBold().setTextSize(14)
            .append(bean.price.toString()).setForegroundColor(Color.parseColor("#E81837")).setBold().setTextSize(18)
            .into(binding.tvPrice)
        binding.tvPriceOld.apply {
            setPaintFlags(binding.tvPriceOld.getPaintFlags() or Paint.STRIKE_THRU_TEXT_FLAG)
        }
        binding.event = this
    }


    fun itemClick(bean: GoodsIndex.JxGoods) {
        context.startActivity(Intent(context, GoodsDetailActivity::class.java).apply {
            putExtra("goodsId", "${bean.goods_id}")
        })
    }

}